import { GraphShapeUtil } from "./GraphShapeUtil";
import { getUid } from "src/util/common";

/**
 * 属性菜单
 */
export class PropertyMenuUtil {
  parsedConfig: { [prop: string]: TreeNode[] };
  propertyTypeNames: Set<string>;
  inited = false
  constructor() {

  }
  init(){
    if (this.inited) return;
    const propertyMenuConfig =  app.activeProject.configManager.getPropertyMenuConfig();

    this.parsedConfig = {};
    this.propertyTypeNames = new Set();

    const keys = Object.keys(propertyMenuConfig);
    keys.forEach(key => {
      if (propertyMenuConfig[key]) {
        this.parsedConfig[key] = propertyMenuConfig[key].map((name: string) => ({
          label: app.activeProject.configManager.getCnNameByTypeName(name),
          icon: "statics/images/sysml/" + name + ".svg",
          value: name
        }));
        propertyMenuConfig[key].forEach(typeName => {
          this.propertyTypeNames.add(typeName);
        });
      }
    });
    this.inited = true;
  }
  /**
   * 获取对应typeName的propertyMenuItem
   * @param typeName
   */
  getMenuItems(typeName: string): Array<TreeNode> | undefined {
    this.init();
    //// typeName = GraphShapeUtil.parseStName(typeName);
    return this.parsedConfig[typeName];
  }

  /**
   * 创建一个虚拟的shape,用于显示addIcon，传入参数为要插入的元素的父shape
   * @param parentShape
   */
  createAddIconShape(parentShape: UmlShape) {
    return {
      owner: parentShape,
      id: getUid(),
      box: {
        position: "absolute",
        isVisible: true,
        expandParent: false,
        width: 12,
        widthUnit: "px",
        height: 12,
        heightUnit: "px",
        paddingTop: 0,
        paddingBottom: 0,
        paddingLeft: 0,
        paddingRight: 0,
        moveType: "notAllow",
        resizeType: "notAllow",
        expandPaddingTop: 0,
        expandPaddingBottom: 0,
        expandPaddingLeft: 0,
        expandPaddingRight: 0,
        right: 0,
        top: 8,
        initX: parentShape.box.boxWidth,
        initY: 8,
        boxX: null,
        boxY: null,
        style:
          "shape=image;html=1;aspect=fixed;image=statics/images/sysml/Canvasiconaddh.svg;movable=0;resizable=0;rotatable=0;cloneable=0;deletable=0;editable=0;expand=0;allowArrows=0;connectable=0;cursor=pointer;"
      },
      shapeType: "addIcon",
      container: parentShape.container
    };
  }
  /**
   * 判断一个typeName是不是Property,(如果新增的元素是Property则需要刷新画布)
   * @param typeName
   */
  isProperty(typeName: string): boolean {
    this.init();
    return this.propertyTypeNames.has(typeName);
  }
}
export const propertyMenuUtil = new PropertyMenuUtil();
